<?php

namespace Biz\module\Classes\mgt;

use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\model\trade\Order as mOrder;
use Biz\module\Classes\core\SearchListAbstract;

use Medoo\Medoo;

class RefundList extends SearchListAbstract
{
    protected function buildSelect()
    {
        $this->select = '*';
    }

    protected function buildWhere()
    {
        $this->where['shop_id'] = $this->params['shop_id'];
        $this->where['rights_status[!]'] = [
            mOrderRefund::RIGHTS_STATUS_NORMAL,
            mOrderRefund::RIGHTS_STATUS_PROOFING
        ];
        if (!empty($this->params['status'])) {
            $this->where['status'] = $this->params['status'];
        }

        if (!empty($this->params['type_refund'])) {
            $this->where['type_refund'] = $this->params['type_refund'];
        }

        if(!empty($this->params['type_order'])){
            if($this->params['type_order'] == 1){
                $status=mOrder::STATUS_WAIT_SEND;
            }
            if($this->params['type_order'] == 2){
                $status=[mOrder::STATUS_SEND_OUT,mOrder::STATUS_SUCCESS];
            }
            $orderList=mOrder::getOrderInfoList(['shop_id'=>$this->where['shop_id'],'status'=>$status],[],[],false);
            if($orderList){
                $orderNos=array_column($orderList,'order_no');
                $this->where['order_no #1']=$orderNos;
            }
        }

        if (!empty($this->params['order_no'])) {
            $this->where['order_no #2'] = htmlspecialchars($this->params['order_no']);
        }

        if (!empty($this->params['refund_order_no'])) {
            $this->where['refund_order_no'] = htmlspecialchars($this->params['refund_order_no']);
        }

        if (!empty($this->params['express_no'])) {
            $this->where['express_no'] = htmlspecialchars($this->params['express_no']);
        }

        if (!empty($this->params['start_apply_time'])) {
            $this->where['addtime[>=]'] = date('Y-m-d H:i:s', strtotime($this->params['start_apply_time']));
        }
        if (!empty($this->params['stop_apply_time'])) {
            $this->where['addtime[<]'] = date('Y-m-d H:i:s', strtotime($this->params['stop_apply_time']));;
        }
    }

    public function getCount()
    {
        return mOrderRefund::count($this->where);
    }

    public function getList()
    {
        $infos = mOrderRefund::select($this->select, array_merge($this->where, ['LIMIT' => $this->limit, 'ORDER' => ['addtime' => 'DESC']]));

        //       \Doraemon\tools\Log::DEBUG("ORDER_REFUND", ["SQL" => mOrderRefund::log()]);
        return $infos;
    }

    public function refundList()
    {
        $data = [];

        $data['count'] = $this->getCount();

        $data['list'] = $this->getList();


        $data['all_count'] = $this->allCount();

        $data['all_count'][0] = mOrderRefund::count([
            'shop_id'          => $this->params['shop_id'],
            'rights_status[!]' => [
                mOrderRefund::RIGHTS_STATUS_NORMAL,
                mOrderRefund::RIGHTS_STATUS_PROOFING
            ]
        ]); //全部状态

        array_walk($data['list'], function (&$v, $k) use (&$data) {

            mOrderRefund::formatInfo($v, 'mgt');

            $data['list'][$k]['sku_info'] = [
                'sku_id'    => $v['sku_id'],
                'goods_id'  => $v['goods_id'],
                'title'     => $v['title'],
                'sku_desc'  => $v['sku_desc'],
                'cover_img' => $v['cover_img_thumb_url']
            ];
        });

        return $data;
    }

    protected function allCount()
    {
        $counts = [
            mOrderRefund::STATUS_NORMAL               => 0, //待审核
            mOrderRefund::STATUS_WAIT_REFUND_GOODS    => 0, //待退货
            mOrderRefund::STATUS_WAIT_CONFIRM_RECEIPT => 0, //待收货
            mOrderRefund::STATUS_COMPLETE             => 0, //退款完成
            mOrderRefund::STATUS_REJECT               => 0 //拒绝退款
        ];
        $allCount = mOrderRefund::select(['counts' => Medoo::raw('COUNT(<refund_order_no>)'), 'status', 'rights_status'], ['GROUP'   => ['status', 'rights_status'],
                                                                                                                           'shop_id' => $this->params['shop_id']
        ]);

        array_walk($allCount, function ($v) use (&$counts) {
            if (key_exists($v['status'], $counts)) {
                if ($v['rights_status'] != mOrderRefund::RIGHTS_STATUS_NORMAL && $v['rights_status'] != mOrderRefund::RIGHTS_STATUS_PROOFING) {
                    $counts[$v['status']] += $v['counts'];
                }
            }
        });
        return $counts;
    }
}